﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using sfsocket.corelib.common;
using sfsocket.corelib.message;
using sfsocket.corelib.connection.client;

namespace chat_client_sp
{
    public partial class ChatClient : Form
    {
        private string m_serverIp;
        private int m_serverPort;
        private ChatClientConnection m_ClientConnection;
        private enum m_ClientState { Unconnect, Connected, Disconnecting };

        //---------------------------------------------------------------------
        // property 
        //---------------------------------------------------------------------
        public ChatClientConnection ClientConnection
        {
            get { return m_ClientConnection; }
        }


        //---------------------------------------------------------------------
        // delegate the update function 
        //---------------------------------------------------------------------
        public delegate void UpdateMessageHistory(string str);
        public void UpdateText(string str)
        {
            textBox_MsgHistory.AppendText("\n" + str + "\n");
            // textBox_MsgHistory.Select();
        }

        //---------------------------------------------------------------------
        //  
        //---------------------------------------------------------------------
        public ChatClient()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            m_serverIp = "127.0.0.1";
            m_serverPort = 8080;
            m_ClientConnection = new ChatClientConnection();
            m_ClientConnection.OnConnected += new EventHandler(OnClientConnected);
            m_ClientConnection.OnMessage += new MsgEventHandler(MessageRecvCallback);
        }

        private void MessageRecvCallback(object sender, MessageEventArgs e)
        {
            object[] para = { e.MSG };
            this.Invoke(new UpdateMessageHistory(UpdateText), para);
        }

        void OnClientConnected(object sender, EventArgs e)
        {
            if (m_ClientConnection.RemoteClientEP != null)
            {
                textBox_MsgHistory.AppendText("Connecting to " + m_ClientConnection.RemoteClientEP.ToString() );
            }
        }

        private void button_SignIn_Click(object sender, EventArgs e)
        {
            if (textBox_Nick.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Input Nickname!");
            }
            m_ClientConnection.Connect(m_serverIp, m_serverPort);
        }

        private void button_Send_Click(object sender, EventArgs e)
        {
            ChatMessage msg = null;
            if (textBox_Message.Text.Trim() == string.Empty)
            {
                
                MessageBox.Show("input message to sent!");
                return;
            }

            if (m_ClientConnection.IsConnected)
            {
                msg = new ChatMessage(textBox_Message.Text);
                m_ClientConnection.SendMsg(msg);
            }
        }

        private void ChatClient_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_ClientConnection.Disconnect();
        }
    }
}
